//
//  GWEmotionGrideView.m
//  新浪微博
//
//  Created by qianfeng on 16/2/20.
//  Copyright (c) 2016年 高炜. All rights reserved.
//

#import "GWEmotionGrideView.h"
#import "GWEmotion.h"
#import "GWEmotionView.h"
#import "GWEmotionPopView.h"
#import "GWEmotionTool.h"
@interface GWEmotionGrideView ()
@property (nonatomic,strong)UIButton *deleteBtn;
@property (nonatomic,copy)NSMutableArray *emotionArray;
@property (nonatomic,strong)GWEmotionPopView *popView;
@end
@implementation GWEmotionGrideView
-(GWEmotionPopView *)popView
{
    if (_popView==nil) {
        _popView =[GWEmotionPopView popView];
    }
    return _popView;
}
-(NSMutableArray *)emotionArray
{
    if (_emotionArray==nil) {
        _emotionArray =[NSMutableArray array];
    }
    return _emotionArray;
}
-(instancetype)initWithFrame:(CGRect)frame
{
    if (self =[super initWithFrame:frame]) {
        
        UIButton *deleteBtn =[[UIButton alloc]init];
        [deleteBtn setImage:[UIImage imageNamed:@"compose_emotion_delete_highlighted"] forState:UIControlStateHighlighted];
        [deleteBtn setImage:[UIImage imageNamed:@"compose_emotion_delete"] forState:UIControlStateNormal];
        self.deleteBtn =deleteBtn;
        [deleteBtn addTarget:self action:@selector(deleteBtnOnToouch) forControlEvents:UIControlEventTouchUpInside];
        
        UILongPressGestureRecognizer *LPGR =[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longTouch:)];
        [self addGestureRecognizer:LPGR];
    }
    return self;
}
//删除按钮被点击
-(void)deleteBtnOnToouch
{
    [[NSNotificationCenter defaultCenter]postNotificationName:GWEmotionClickDeleteBtnNotification object:nil];
}
-(void)longTouch:(UILongPressGestureRecognizer *)LPGR
{
    CGPoint currentPoint =[LPGR locationInView:LPGR.view];
    GWEmotionView *emotionView =[self emotionViewWithCGPoint:currentPoint];
    
    if (LPGR.state ==UIGestureRecognizerStateEnded) {
        
        [self.popView dismiss];
        [self postEmotionSelectedNotification:emotionView];
    }
    else
    {
       [self.popView popViewShowWithEmotionView:emotionView];
    }
    
}

-(GWEmotionView *)emotionViewWithCGPoint:(CGPoint)point
{
   __block GWEmotionView *foundEmotionView =nil;
    [self.emotionArray enumerateObjectsUsingBlock:^(GWEmotionView *emotionView, NSUInteger idx, BOOL *stop) {
        if(CGRectContainsPoint(emotionView.frame, point)&&emotionView.hidden ==NO)
        {
            foundEmotionView =emotionView;
            *stop =YES;
        }
    }];
    return foundEmotionView;
}

-(void)setEmotions:(NSArray *)emotions
{
    _emotions =emotions;
    
    int count =(int)emotions.count;
    int currentCount =(int)self.subviews.count;
    for (int i=0; i<count; i++) {
        GWEmotionView *emmtionView =nil;
        if (i>=currentCount) {
            emmtionView=[[GWEmotionView alloc]init];
            [emmtionView addTarget:self action:@selector(emotionOnClick:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:emmtionView];
            [self.emotionArray addObject:emmtionView];
        }
        else
        {
            emmtionView =self.subviews[i];
        }
        emmtionView.emotion =emotions[i];
        emmtionView.hidden =NO;
    }
    
    for (int i =count; i<currentCount-1; i++) {
        GWEmotionView *emmtionView =self.emotionArray[i];
        emmtionView.hidden =YES;
    }
    
    [self addSubview:self.deleteBtn];
    
}
-(void)emotionOnClick:(GWEmotionView *)emotionView
{
    
    [self.popView popViewShowWithEmotionView:emotionView];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.popView dismiss];
        
        [self postEmotionSelectedNotification:emotionView];
    });
    
    
}
//发出选中表情的通知
-(void)postEmotionSelectedNotification:(GWEmotionView *)emotionView
{
    if(emotionView)
    {
        [GWEmotionTool addEmotionWithEmotion:emotionView.emotion];
        [[NSNotificationCenter defaultCenter]postNotificationName:GWEmotionDidSelectedNotification object:nil userInfo:@{GWDidSelectedEmotion:emotionView.emotion}];
    }
}
-(void)layoutSubviews
{
    
    [super layoutSubviews];
    int count =(int)self.emotionArray.count;
    CGFloat marginX =15;
    CGFloat marginY =15;
    CGFloat emotionW = (self.width -marginX*2)/GWEmotionMaxColCount;
    CGFloat emotionH = (self.height -marginY)/GWEmotionMaxRowCount;
    for (int i =0; i<count; i++) {
        GWEmotionView *emotionView = self.emotionArray[i];
        
        emotionView.frame =CGRectMake(marginX+i%GWEmotionMaxColCount*emotionW,marginY + i/GWEmotionMaxColCount*emotionH, emotionW, emotionH);
    }
    
    self.deleteBtn.frame =CGRectMake(self.width-emotionW-marginX, self.height-emotionH, emotionW, emotionH);
}
@end
